<?php
namespace app\modules\supplier\logic;
use app\modules\base\LogicBase;
use think\Db;
/**
* 
*/

class SupplierApplyLogic extends LogicBase
{
	
     //验证
     protected $valid;

      public function _initialize()
    {

        $this->valid = validate('app\modules\supplier\validate\SupplierApplyPost'); //游客验证
    }
	 /**
     * 添加供应商申请
     * @param array $data 参数
     * @return bool|array
     */

    public function add($data = [])
    {

        // Db::startTrans();//开启事物

        if (!$this->valid->check($data)) {//字段验证
            $this->_error[] = $this->valid->getError();
            return false;
        }else{
            //避免重复申请 未审核情况
            if (Db::name('supplier_apply')->where(['supplier_name'=>$data['supplier_name'],'charge_phone'=>$data['charge_phone'],'idcard'=>$data['idcard'],'apply_status'=>1])->find()) {
                $this->_error[] ='您已经申请过了，请等待审核！';
                return false;
            }
            //避免重复申请 审核通过情况
            if (Db::name('supplier_apply')->where(['supplier_name'=>$data['supplier_name'],'charge_phone'=>$data['charge_phone'],'idcard'=>$data['idcard'],'apply_status'=>2])->find()) {
                $this->_error[] ='您已经成功入驻了供应商系统！';
                return false;
            }
            //避免重复申请 审核通过情况
            if (Db::name('supplier')->where(['supplier_name'=>$data['supplier_name'],'charge_phone'=>$data['charge_phone'],'idcard'=>$data['idcard']])->find()) {
                $this->_error[] ='您已经成功入驻了供应商系统！';
                return false;
            }
            //首次确保账户唯一性
            if (Db::name('supplier_apply')->where(['username'=>$data['username'],'apply_status'=>2])->find()) { 
                $this->_error[]='供应商账号已存在！';
                return false;
            }

        }
        
//        $data['password'] = md5($data['password']);

        
        $res = Db::name('supplier_apply')->insert($data);

        // echo  Db::name('supplier_apply')->getLastSql(); exit();

        if ($res) {
            return true;
        }else{
            return false;
        }

    }
}